//
//  NSString+Tool.m
//  LuxuryGoods
//
//  Created by 刘星辰 on 2018/5/19.
//  Copyright © 2018年 Future. All rights reserved.
//

#import "NSString+Tool.h"

@implementation NSString (Tool)
/**
 判断字符串是否为空
 
 @param string 字符串
 @return 布尔值，YES OR NO
 以下几种情况都是返回NO
 nil NULL NSNull "<null>" "" "  "
 */
- (BOOL)isNotBlank {
    if (self == nil || self == NULL) {
        return NO;
    }
    if ([self isKindOfClass:[NSNull class]]) {
        return NO;
    }
    if ([self isKindOfClass:[NSString class]]) {
        if ([self isEqualToString:@"<null>"] || [self isEqualToString:@"(null)"]) {
            return NO;
        }
        if ([[self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length] == 0) {
            return NO;
        }
    }
    return YES;
}


/**
 根据判断字符串是否为空返回""
 
 @param string 传进来的字符串
 @return NSString 返回的结果字符串
 */
- (NSString *)blankString {
    if (![self isNotBlank])
    {
        return @"";
    }
    return self;
}

#pragma mark - 校验密码
- (BOOL)isVerifyMobile
{
    NSString * phoneRegex = @"^(0|86|17951)?(13[0-9]|15[012356789]|17[0-9]|18[0-9]|14[0-9])[0-9]{8}$";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", phoneRegex];
    return [pred evaluateWithObject:self];
}

#pragma mark - 判断密码
- (BOOL)isValidPwd
{
    BOOL flag = YES;
    if (self.length < 6 || self.length > 16 || ([self rangeOfString:@" "].location != NSNotFound)) {
        flag = NO;
    }
    return flag;
}

#pragma mark 校验email
- (BOOL)isValidateEmail
{
    //邮箱的格式
    NSString * patternEmail = @"^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9])+\.)+([a-zA-Z0-9]{2,4})+$";
    NSError *err = nil;
    NSRegularExpression *EmailExp =[NSRegularExpression regularExpressionWithPattern:patternEmail options:0 error:&err];
    
    NSTextCheckingResult *isMatchEmail = [EmailExp firstMatchInString:self options:0 range:NSMakeRange(0, [self length])];
    
    if (isMatchEmail) {
        return YES;
    }
    return NO;
}

#pragma mark - 校验日期
- (BOOL)isValidateDate
{
    NSString *patternDate = @"^(?:(?!0000)[0-9]{4}-(?:(?:0[1-9]|1[0-2])-(?:0[1-9]|1[0-9]|2[0-8])|(?:0[13-9]|1[0-2])-(?:29|30)|(?:0[13578]|1[02])-31)|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)-02-29)$";
    NSError *err = nil;
    NSRegularExpression *dateExp =[NSRegularExpression regularExpressionWithPattern:patternDate options:0 error:&err];
    
    NSTextCheckingResult *isMatchDate = [dateExp firstMatchInString:self options:0 range:NSMakeRange(0, [self length])];
    
    if (isMatchDate) {
        return YES;
    }
    return NO;
}


/**
 截取字符串 处理了，纯汉字，纯英文，混汉英，带空格末尾附带省略号
 */
- (NSString *)subStringWithLength:(NSInteger)length
{
    NSInteger sum = 0;
    
    NSString *subStr = [[NSString alloc] init];
    
    for(int i = 0; i<[self length]; i++){
        
        unichar strChar = [self characterAtIndex:i];
        
        //需要处理，纯汉字，纯英文，混汉英，带空格
        //汉字两个字符，字母一个字符，空格一个字符
        if(strChar < 256){
            sum += 1;
        }
        else {
            sum += 2;
        }
        if (sum >= length) {
            
            subStr = [self substringToIndex:i+1];
            return [subStr stringByAppendingString:@"..."];
        }
    }
    
    return subStr;
}

/**
 计算转换成C字符串后的长度
 */
- (NSInteger)getCStringLength
{
    NSInteger strlength = 0;
    char* p = (char*)[self cStringUsingEncoding:NSUnicodeStringEncoding];
    for (NSInteger i = 0 ; i<[self lengthOfBytesUsingEncoding:NSUnicodeStringEncoding] ;i++) {
        if (*p) {
            p++;
            strlength++;
        }
        else {
            p++;
        }
    }
    return strlength;
}

@end
